package com.hubu.jwxt.controller;

import com.hubu.jwxt.beans.SingleSubject;
import com.hubu.jwxt.service.StudentService;
import com.hubu.jwxt.utils.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

import static java.lang.System.out;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/student/list")
    public String list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum, @RequestParam(value = "Cid") int Cid , Model model){
        PageHelper<SingleSubject> pageHelper = studentService.queryCourses(pageNum, 5,Cid);
        model.addAttribute("pageHelper",pageHelper);
        return "/student-list.html";
    }

    @RequestMapping("/student/addgrade")
    public String update(String Sname, int Cid, String grade, HttpServletRequest request){

        //request.setAttribute("msg","");
        int result = studentService.entryScore(Sname,Cid,grade);
        if(result!=0)
        {
            out.println("<script>alert('AddSucceed')</script>");
            return "redirect:/student/list?pageNum=1&Cid="+Cid;
        }
        else{

            return "redirect:/student/list?pageNum=1&Cid="+Cid;
        }
    }
}
